#include <bits/stdc++.h>
#include <cstdio>
using namespace std;
struct node
{
    int num;
    int id;
    bool operator < (const node &rhs) const{
        return num<rhs.num;
    }
}b[2005];
struct node2
{
    int num;
    int color=0;
}a[2005];
int main()
{
    int n,score=0,t,beg,e;
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        score=0;
        for (int i=1;i<=n;i++)
        {
            scanf("%d",&a[i].num);
            b[i].num=a[i].num;
            b[i].id=i;
        }
        sort(b+1,b+1+n);
        for (int i=1;i<n;i++)
        {
            if(b[i].num==b[i+1].num)
            {
                beg=b[i].id,e=b[i+1].id;
                score+=a[beg].num;
                for (int j=beg+1;j<e;j++)
                {
                    a[j].color=!a[beg].color;
                }
            }
        }
        cout << score << endl;
    }
    fclose(stdin);
    fclose(stdout);
    return 0;
}
